﻿

using MoFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MoFramework
{
    public class UILayer
    {
        const int layerCountPerLayer = 1000;
        public List<BaseUIForm> m_FormsList = new List<BaseUIForm>();

        public int layerOrder;

        RectTransform m_TransLayer;
        public void Init(GameObject gObjroot,string name)
        {
            m_TransLayer = (new GameObject(name)).AddComponent<RectTransform>();
            m_TransLayer.SetParent(gObjroot.transform,false);
            m_TransLayer.anchorMax = Vector2.one;
            m_TransLayer.anchorMin = Vector2.zero;
        }

        public void OpenUIForm(BaseUIForm form,object param = null)
        {
            int formOrder = CloseUIForm(form);

            form.transform.SetParent(m_TransLayer.transform,false);
            form.order = layerOrder + formOrder;
            form.Open(param);

            m_FormsList.Insert(0, form);
        }

        public int CloseUIForm(BaseUIForm form)
        {
            int sortOrder = 0; ;
            int length = m_FormsList.Count;

            for (int idx = length - 1; idx >= 0; --idx)
            {
                BaseUIForm oppenedForm = m_FormsList[idx];
                if (form == oppenedForm)
                {
                    m_FormsList.RemoveAt(idx);
                }else
                {
                    oppenedForm.order = sortOrder + layerOrder;
                    sortOrder += oppenedForm.requireLayer;
                }
            }
            return sortOrder;
        }

    }
}
